package com.qf.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.openapi.web.master.dao.SysDao;
import com.qf.openapi.web.master.entity.Sys;
import com.qf.openapi.web.master.service.SysService;
import com.qf.openapi.web.master.utils.Constants;
import com.qf.openapi.web.master.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
@Slf4j
public class SysServiceImpl implements SysService {

    @Autowired
    private SysDao sysDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo<Sys> getSysByCondition(Integer page, Integer limit, Sys sys) {
        PageHelper.startPage(page, limit);
        List<Sys> data = sysDao.getSysByCondition(sys);
        final PageInfo<Sys> pageInfo = new PageInfo<>(data);
        final long total = pageInfo.getTotal();
        return ResultVo.ok((int) total, data);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo addSys(Sys sys) {
        sysDao.insertSys(sys);
        return new ResultVo(Constants.SUCCESS_CODE, "添加用户成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo deleteSysByIds(List<Integer> ids) {
        sysDao.deleteSysByIds(ids);
        return new ResultVo(Constants.SUCCESS_CODE, "删除成功");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo updateUser(Sys sys) {
        sysDao.updateSys(sys);
        return new ResultVo(Constants.SUCCESS_CODE, "修改用户成功");
    }
}
